package com.tarena.lbs.service.impl;

import com.tarena.lbs.entity.CouponActivity;
import com.tarena.lbs.enums.ActivityStatusEnum;
import com.tarena.lbs.mapper.CouponActivityMapper;
import com.tarena.lbs.service.CouponActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class CouponActivityServiceImpl implements CouponActivityService {

    @Autowired
    private CouponActivityMapper couponActivityMapper;

    @Override
    public CouponActivity getActivityById(Integer id) {
        return couponActivityMapper.getActivityById(id);
    }

    @Override
    @Transactional
    public boolean updateActivityStatus(Integer id, Integer status) {
        int result = couponActivityMapper.updateActivityStatus(id, status);
        return result > 0;
    }

    @Override
    public List<CouponActivity> listActivitiesByUserGroup(String userGroup) {
        return couponActivityMapper.listActivitiesByUserGroup(userGroup, ActivityStatusEnum.ENABLED.getCode());
    }

    @Override
    public boolean isActivityValid(Integer activityId) {
        if (activityId == null) {
            return false;
        }
        CouponActivity activity = getActivityById(activityId);
        if (activity == null) {
            return false;
        }
        // 检查活动状态
        if (!activity.getStatus().equals(ActivityStatusEnum.ENABLED.getCode())) {
            return false;
        }
        // 检查活动时间
        Date now = new Date();
        if (now.before(activity.getStartTime()) || now.after(activity.getEndTime())) {
            return false;
        }
        return true;
    }
}